package com.cg.service.impl;

import com.cg.dao.AreaMapper;
import com.cg.entity.Area;
import com.cg.service.AreaService;
import com.cg.vo.AreaListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Area)表服务实现类
 *
 * @author chengong
 * @since 2024-01-03 17:26:42
 */
@Service("areaService")
public class AreaServiceImpl implements AreaService {
    @Resource
    private AreaMapper areaMapper;


    /**
     * 查询省
     */
    @Override
    public List<Area> queryShen() {
        return areaMapper.queryShen();
    }

    /**
     * 查询市
     */
    @Override
    public List<Area> queryShi(Integer areaId) {
        return areaMapper.queryShi(areaId);
    }

    /**
     * 查询区
     */
    @Override
    public List<Area> queryQu(Integer areaId) {
        return areaMapper.queryQu(areaId);
    }

    /**
     * 省市区级联，查询省市区
     */
    @Override
    public List<AreaListVo> getAreas() {
        /*List<AreaListVo> list = areaMapper.getAreas()
                .stream()
                .map(area -> CglibUtil.copy(area, AreaListVo.class))
                .collect(Collectors.toList());*/
        List<AreaListVo> list = areaMapper.getAreas();
        //获取一级菜单--省
        List<AreaListVo> shenList = list.stream()
                .filter(shen -> Objects.equals(shen.getParentId(), "-1"))
                .collect(Collectors.toList());
        //遍历省
        shenList.forEach(oneItem -> {
            //获取二级菜单--市
            List<AreaListVo> shiList = list.stream()
                    .filter(shi -> shi.getParentId().intValue() == oneItem.getId().intValue())
                    .collect(Collectors.toList());
            //遍历二级菜单
            shiList.forEach(twoItem -> {
                //获取三级菜单--区
                List<AreaListVo> quList = list.stream()
                        .filter(qu -> qu.getParentId().intValue() == twoItem.getId().intValue())
                        .collect(Collectors.toList());
                twoItem.setChildAreaList(quList);
            });
            oneItem.setChildAreaList(shiList);
        });
        return list;
    }


}
